#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "MyRTC.h"
#include "Convert.h"
#include "Key.h"
#include "Timer.h"

#define YEAR 	1
#define MONTH 	2
#define DAY 	3
#define HOUR 	4
#define MIN		5
#define SENCEND	6


void OLED_Proc(void);
void KEY_Proc(void);

char OLED_Show_text[30];	//定义LCD显示 内容
unsigned char KeyNumber;
uint32_t Timer_1ms;
uint32_t OLED_Tick;
unsigned char view_menu;

unsigned char oled_flag_off;
unsigned char set_temp_flag;
unsigned char set_time_ok;

int main(void)
{
	/*模块初始化*/
	OLED_Init();		//OLED初始化
	MyRTC_Init();		//RTC初始化
	Key_Init();
	Timer_Init();
	
	Convert_to_Lunar();
	
	/*显示静态字符串*/
	OLED_ShowString(1, 1, "    Calendar");
	OLED_ShowString(2, 1, "Solar:XXXX-XX-XX");
	OLED_ShowString(3, 1, "Lunor:XXXX-XX-XX");
	OLED_ShowString(4, 1, "Time :XX:XX:XX");	
	
	/*开启时钟*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);		//开启PWR的时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);		//开启BKP的时钟
	
	/*备份寄存器访问使能*/
	PWR_BackupAccessCmd(ENABLE);							//使用PWR开启对备份寄存器的访问
	
	while (1)
	{		
		KeyNumber = Key_GetNum();
		
		OLED_Proc();
		KEY_Proc();	
		
		MyRTC_ReadTime();							//RTC读取时间，最新的时间存储到MyRTC_Time数组中		
		
		if (MyRTC_Time[3] == 0 && MyRTC_Time[4] == 0 && MyRTC_Time[5] == 0)
		{
			hjz lunar = toLunar(osolar); // 阳历转阴历
			Convert_to_Lunar();
		}
		
//		OLED_ShowNum(1, 15, KeyNumber, 1);
	
		
	}
}


void OLED_Proc(void)
{
	//函数减速
	if (Timer_1ms - OLED_Tick < 300)
		return;
	OLED_Tick = Timer_1ms;
	
	oled_flag_off = !oled_flag_off;
	
	if (view_menu == 0)	//最初的界面
	{
		sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", MyRTC_Time[0], MyRTC_Time[1], MyRTC_Time[2]);	//年月日 - 阳历
		OLED_ShowString(2, 1, OLED_Show_text);			
		sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月日 - 阳历
		OLED_ShowString(3, 1, OLED_Show_text);				
		sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", MyRTC_Time[3], MyRTC_Time[4], MyRTC_Time[5]);	//年月日 - 阳历
		OLED_ShowString(4, 1, OLED_Show_text);
		set_temp_flag = 1;	//
	}
	else
	{
		if (oled_flag_off == 0)
		{
			sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
			OLED_ShowString(2, 1, OLED_Show_text);			
			sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月日 - 阳历
			OLED_ShowString(3, 1, OLED_Show_text);					
			sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", Set_time_temp[3], Set_time_temp[4], Set_time_temp[5]);	//年月日 - 阳历
			OLED_ShowString(4, 1, OLED_Show_text);
		}
		else if (oled_flag_off == 1)
		{			
			if (view_menu == YEAR)
			{			
				if (set_temp_flag == 1)	//进来赋值一次
				{
					Set_time_temp[0] = MyRTC_Time[0];
					Set_time_temp[1] = MyRTC_Time[1];
					Set_time_temp[2] = MyRTC_Time[2];
					Set_time_temp[3] = MyRTC_Time[3];
					Set_time_temp[4] = MyRTC_Time[4];
					Set_time_temp[5] = MyRTC_Time[5];
					
					set_temp_flag = 0;
				}
				
				sprintf(OLED_Show_text, "Solar:    -%02d-%02d", Set_time_temp[1], Set_time_temp[2]);	// 月日 - 阳历
				OLED_ShowString(2, 1, OLED_Show_text);				
				sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月日 - 阳历
				OLED_ShowString(3, 1, OLED_Show_text);						
				sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", Set_time_temp[3], Set_time_temp[4], Set_time_temp[5]);	//
				OLED_ShowString(4, 1, OLED_Show_text);					
			}
			else if (view_menu == MONTH) 
			{	
				sprintf(OLED_Show_text, "Solar:%04d-  -%02d", Set_time_temp[0], Set_time_temp[2]);	//年 日 - 阳历
				OLED_ShowString(2, 1, OLED_Show_text);
				sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月 - 阳历
				OLED_ShowString(3, 1, OLED_Show_text);						
				sprintf(OLED_Show_text, "Time: %02d:%02d:%02d", Set_time_temp[3], Set_time_temp[4], Set_time_temp[5]);	//
				OLED_ShowString(4, 1, OLED_Show_text);	
			}
			else if (view_menu == DAY ) 
			{	
				sprintf(OLED_Show_text, "Solar:%04d-%02d-  ", Set_time_temp[0], Set_time_temp[1]);	//年月 - 阳历
				OLED_ShowString(2, 1, OLED_Show_text);
			}
			else if (view_menu == HOUR) 
			{	
				sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
				OLED_ShowString(2, 1, OLED_Show_text);
				
				sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月日 - 阳历
				OLED_ShowString(3, 1, OLED_Show_text);		
				
				sprintf(OLED_Show_text, "Time:   :%02d:%02d", Set_time_temp[4], Set_time_temp[5]);	//年月日 - 阳历
				OLED_ShowString(4, 1, OLED_Show_text);
			}
			else if (view_menu == MIN) 
			{
				sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
				OLED_ShowString(2, 1, OLED_Show_text);
				
				sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月日 - 阳历
				OLED_ShowString(3, 1, OLED_Show_text);		
				
				sprintf(OLED_Show_text, "Time: %02d:  :%02d", Set_time_temp[3], Set_time_temp[5]);	//年月日 - 阳历
				OLED_ShowString(4, 1, OLED_Show_text);
			}
			else if (view_menu == SENCEND) 
			{	
				sprintf(OLED_Show_text, "Solar:%04d-%02d-%02d", Set_time_temp[0], Set_time_temp[1], Set_time_temp[2]);	//年月日 - 阳历
				OLED_ShowString(2, 1, OLED_Show_text);
				
				sprintf(OLED_Show_text, "Lunor:%04d-%02d-%02d", My_lunar[0], My_lunar[1], My_lunar[2]);	//年月日 - 阳历
				OLED_ShowString(3, 1, OLED_Show_text);		
				
				sprintf(OLED_Show_text, "Time: %02d:%02d:  ", Set_time_temp[3], Set_time_temp[4]);
				OLED_ShowString(4, 1, OLED_Show_text);
			}
		}
	}	

}

void KEY_Proc(void)
{
	if 		(KeyNumber == 1)
	{
		view_menu ++;
		if (view_menu  == 7) view_menu = 0;	//7个界面

	}
	
	
	else if (KeyNumber == 2)
	{

		if (view_menu == YEAR)			Set_time_temp[0]++;
		else if (view_menu == MONTH) 	Set_time_temp[1]++;
		else if (view_menu == DAY )		Set_time_temp[2]++;
		else if (view_menu == HOUR) 	Set_time_temp[3]++;
		else if (view_menu == MIN) 		Set_time_temp[4]++;
		else if (view_menu == SENCEND)	Set_time_temp[5]++;
	}
	else if (KeyNumber == 3)
	{
		if (view_menu == YEAR)			Set_time_temp[0]--;
		else if (view_menu == MONTH) 	Set_time_temp[1]--;
		else if (view_menu == DAY )		Set_time_temp[2]--;
		else if (view_menu == HOUR) 	Set_time_temp[3]--;
		else if (view_menu == MIN) 		Set_time_temp[4]--;
		else if (view_menu == SENCEND)	Set_time_temp[5]--;
	}
	else if (KeyNumber == 4)
	{
		view_menu = 0;
		MyRTC_Time[0] = Set_time_temp[0];
		MyRTC_Time[1] = Set_time_temp[1];
		MyRTC_Time[2] = Set_time_temp[2];
		MyRTC_Time[3] = Set_time_temp[3];
		MyRTC_Time[4] = Set_time_temp[4];
		MyRTC_Time[5] = Set_time_temp[5];
		
		MyRTC_SetTime();
		Convert_to_Lunar();
	}
}

void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)	//更新中断标志位==SET
	{
		Timer_1ms ++;
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);	//清除标志位
	}
}
